package com.meet.bizservice.impl;

import com.meet.persist.dao.FriendshipDao;
import com.meet.persist.dao.PrivateMessageDao;
import com.meet.api.dto.response.MeetUserResponseDTO;
import com.meet.persist.entity.PrivateMessage;
import com.meet.exception.BusinessException;
import com.meet.bizservice.SocialService;
import com.meet.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class SocialServiceImpl implements SocialService {

    private static final Logger logger = LoggerFactory.getLogger(SocialServiceImpl.class);

    @Autowired
    private FriendshipDao friendshipDao;

    @Autowired
    private PrivateMessageDao privateMessageDao;

    @Override
    public Result<List<MeetUserResponseDTO>> getFriendsList(int page, int limit) {
        try {
            logger.info("Getting friends list for page: {}, limit: {}", page, limit);

            // 这里应该从JWT中获取用户ID，为简化演示直接返回示例数据
            return Result.success(Arrays.asList());
        } catch (Exception e) {
            logger.error("Error getting friends list", e);
            throw new BusinessException("获取好友列表失败");
        }
    }

    @Override
    public Result<Boolean> addFriend(String friendId) {
        try {
            logger.info("Adding friend with ID: {}", friendId);

            // 这里应该从JWT中获取用户ID，为简化演示直接返回成功
            return Result.success(true);
        } catch (Exception e) {
            logger.error("Error adding friend", e);
            throw new BusinessException("添加好友失败");
        }
    }

    @Override
    public Result<String> sendPrivateMessage(String receiverId, String content) {
        try {
            logger.info("Sending private message to user: {}", receiverId);

            // 这里应该从JWT中获取用户ID，为简化演示直接返回示例数据
            String messageId = UUID.randomUUID().toString().replace("-", "");

            // 创建私信记录（实际项目中应该保存到数据库）
            PrivateMessage message = new PrivateMessage();
            message.setMessageId(messageId);
            message.setSenderId("1234567890"); // 从JWT中获取
            message.setReceiverId(receiverId);
            message.setContent(content);

            // 保存到数据库（这里简化处理）
            // privateMessageDao.insert(message);

            return Result.success(messageId);
        } catch (Exception e) {
            logger.error("Error sending private message", e);
            throw new BusinessException("发送私信失败");
        }
    }

    @Override
    public Result<List<PrivateMessage>> getMessagesList(int page, int limit) {
        try {
            logger.info("Getting messages list for page: {}, limit: {}", page, limit);

            // 这里应该从JWT中获取用户ID，为简化演示直接返回示例数据
            return Result.success(Arrays.asList());
        } catch (Exception e) {
            logger.error("Error getting messages list", e);
            throw new BusinessException("获取消息列表失败");
        }
    }
}
